#!/usr/bin/env node
const program = require('commander');
const version = require('./package.json').version;
const {getGitBranchName} = require('./index');

program.version(version);
program.command('include <branches>')
  .description('允许在限制的分支中执行，多个分支以","符号分割')
  .action(branches => {
    branches = [...new Set(branches.split(/[,，]/))];
    let current = getGitBranchName();
    if (!current) {
      process.exit(1);
    }
    if (!branches.includes(current)) {
      console.error(`✘ 该命令只允许在 ${branches.join(',')} 分支执行，当前分支 ${current} 不允许执行当前命令！！`.red);
      process.exit(1);
    } else {
      console.log(`✔ 分支检查通过，可执行命令`.green);
      process.exit(0);
    }
  });

program.command('exclude <branches>')
  .description('允许在排除的分支中执行，多个分支以","符号分割')
  .action(branches => {
    branches = [...new Set(branches.split(/[,，]/))];
    let current = getGitBranchName();
    if (!current) {
      process.exit(1);
    }
    if (branches.includes(current)) {
      console.error(`✘ 该命令不允许在 ${branches.join(',')} 分支执行，当前分支 ${current} 不允许执行当前命令！！`.red);
      process.exit(1);
    } else {
      console.log(`✔ 分支检查通过，可执行命令`.green);
      process.exit(0);
    }
  });

program.parse();
